package com.augurit.wf.common.service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.model.Activity;
import org.jbpm.api.model.ActivityCoordinates;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.task.TaskImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.base.form.Attachment;
import com.augurit.common.base.form.ResultForm;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.bi.web.form.WfPrivControlForm;
import com.augurit.wf.client.summary.web.form.WfTaskSummaryForm;
import com.augurit.wf.common.entity.Jbpm4Task;
import com.augurit.wf.common.extmodel.SummaryCountNode;
import com.augurit.wf.common.form.TraceRecord;
import com.augurit.wf.common.form.WfBusBaseEntity;
import com.augurit.wf.common.web.WfBusTemplateViewConfig;

public interface IWfService {
	
	/**
	 * 获取流程定义key对应的最新版本
	 * @param procDefKey
	 * @return
	 */
	public Integer getLastVersion(String procDefKey);
	
	/**
	 * 起草阶段，根据templateCode设置流程实体的运行信息
	 * @param instance 
	 * @param templateCode
	 * @return
	 */
	public WfBusInstanceTaskForm setInstanceRunInfoByTemplateCode(WfBusInstanceTaskForm instance, final String templateCode);
	
	/**
	 * 根据taskInstDbid设置流程实体的运行信息
	 * @param instance 
	 * @param logineUserName
	 * @return
	 */
	public WfBusInstanceTaskForm setInstanceRunInfoByTaskId(WfBusInstanceTaskForm instance, final String logineUserName);
	
	/**
	 * 根据wfBusInstanceId设置流程实体的运行信息
	 * @param instance 
	 * @return
	 */
	public WfBusInstanceTaskForm setInstanceRunInfoByWfInstancId(WfBusInstanceTaskForm instance);
	
	public void wfDraft(String templateCode, WfBusInstanceTaskForm instance);
	
	/**
	 * 根据流程key和流程版本号获取流程定义
	 * @param procDefKey
	 * @param version
	 * @return
	 */
	public ProcessDefinition getProcDefByProcDefKeyAndVersion(String procDefKey, Integer version);
	
	public ProcessDefinitionImpl getProcDefByJbpm4Task(Jbpm4Task jbpm4Task);
	
	public ProcessDefinitionImpl getProcDefByTaskInstDbid(Long taskInstDbid);
	
	public ProcessDefinitionImpl getProcDefByProcInstId(String executionId);

	
	public <T extends WfBusBaseEntity> WfBusInstanceTaskForm wfBusStart(String templateCode, T form, String startWfUserKey, Map<String, Object> variables);
	
//	public ProcessInstance startWfBusInstance(String templateCode, String busEntityName, String busEntityKey, String procDefKey, Integer version, Map<String, ?> variables);
	
	/**
	 * 从当前类路径中的XML文件中部署流程定义
	 * @param processDefFilePath 流程定义文件路径
	 * @return
	 */
	public String deployByXmlFileFromClasspath(String processDefFilePath);
	
	/**
	 * 从客户端上传的XML文件部署流程定义
	 * @param attachment 附件
	 * @return
	 */
	public String deployByXmlFileFromAttachment(Attachment attachment);
	
	/**
	 * 从客户端上传的zip文件中(zip文件包含流程定义xml文件、流程图png文件)部署流程定义
	 * @param attachment 附件
	 * @return
	 * @throws Exception
	 */
	public String deployByZipFileFromAttachment(Attachment attachment)throws Exception;
	
	/**
	 * 根据zip文件流发布流程(zip文件包含流程定义xml文件、流程图png文件)部署流程定义
	 * @param zipInputStream zip文件流
	 * @return
	 * @throws Exception
	 */
	public String deployByZipFileByZipInputStream(ZipInputStream zipInputStream)throws Exception;
	
	/**
	 * 根据流程定义key和流程版本号获取其对应的xml文件流
	 * @param procDefKey 流程定义key
	 * @param version	流程版本
	 * @return InputStream 文件流
	 */
	public InputStream getProcDefXmlFileAsStream(String procDefKey, Integer version);
	

	
	/**
	 * 根据流程实例id获取其流程图对应的png文件流
	 * @param processInstanceId 流程实例id
	 * @return InputStream 文件流
	 */
	@Transactional(readOnly = true)
	public InputStream getProcDiagramPngFileAsStream(String processInstanceId);
	
	/**
	 * 获取流程实例所处的流程结点名称
	 * @param processInstanceId 流程实例id
	 * @return 流程节点名称
	 */
	public String getActivityNameOfProcessInstance(String processInstanceId);
	
	/**
	 * 根据流程定义key和流程版本号获取其对应的流程实例
	 * @param procDefKey 流程定义key
	 * @param version 流程版本
	 * @return 流程实例列表
	 */
	public List<ProcessInstance> getAllProcessInstances(String procDefKey, Integer version);
	
	/**
	 * 根据流程定义Id获取其对应的流程实例
	 * @param processDefId 流程定义Id
	 * @return 流程实例列表 
	 */
	public List<ProcessInstance> getProcInstsByTemplateId(Long wfTemplateId);
	
	/**
	 * 根据deploymentId获取流程定义
	 * @param deploymentId
	 * @return 流程定义对象 
	 */
	public ProcessDefinition getProcDefByDeploymentId(String deploymentId);
	
	/**
	 * 删除流程指定版本，并级联删除其对应的流程实例
	 * @param procDefKey 流程定义key
	 * @param version 流程版本
	 */
	public void deleteProcessVersionCascade(String procDefKey, Integer version);
	
	/**
	 * 启动流程实例
	 * @param procDefKey 流程定义KEY
	 * @param variables 变量
	 * @return
	 */
	public ProcessInstance startProcessInstance(String procDefKey, Integer version, Map<String, ?> variables);
	
	public WfBusInstanceTaskForm getWfBusInstanceById(Long wfBusInstanceId);
	
	/**
	 * 获取指定流程实例的指定用户的未签收或处理中的任务列表
	 * @param executionId
	 * @param userKey
	 * @return
	 */
	public List<Task> getPersonalTasks(String executionId, String userKey);
	
	/**
	 * 完成指定任务
	 * @param taskInstDbid 任务ID
	 */
	public void completeTask(String taskInstDbid);
	
	/**
	 * 根据流程业务实例ID获取流程业务实例对象
	 * @param wfBusInstanceId 流程业务实例ID
	 */
	public WfBusInstanceTaskForm getWfBusInstanceTask(Long wfBusInstanceId);
	
	/**
	 * 根据用户标识获取指定用户的流程业务实例对象
	 * @param userKey 用户标识
	 */
	public List<WfBusInstanceTaskForm> getPersonalTasks(String userKey);
	
	public Task getUniquePersonalTask(String executionId, String userKey);
	
	public void loadDbTasksWithBusInfo(Page page, String templateCode, String userKey, Map<String, String> params);
	
	public void loadZbTasksWithBusInfo(Page page, String templateCode, String userKey, Map<String, String> params);
	
	public void loadDZbTasksWithBusInfo(Page page, String templateCode, String userKey, Map<String, String> params);
	
	public void loadYbTasksWithBusInfo(Page page, String templateCode, String userKey, Map<String, String> params);
	
	public void loadBjTasksWithBusInfo(Page page, String templateCode, String userKey, Map<String, String> params);
	
	public WfBusTemplateViewConfig loadViewConfig(String templateCode);
	
	public WfBusTemplateViewConfig loadViewConfig(Long templateId);
	
	public void loadWfSendOption(WfBusInstanceTaskForm form);
	
	/**
	 * 返回流程定义中开始（或第一个）人工节点
	 * 前提条件：流程定义的开始节点的下一节点只能是单个人工节点
	 * @param processDefinitionId
	 * @return
	 */
	public Activity getStartTaskActivity(String processDefinitionId);
	
	/**
	 * 返回流程定义中开始（或第一个）人工节点名称
	 * 前提条件：流程定义的开始节点的下一节点只能是单个人工节点
	 * @param processDefinitionId
	 * @return
	 */
	public String getStartTaskActivityName(String processDefinitionId);
	
	/**
	 * 根据流程定义key获取流程的所有节点
	 * @param processDefinitionKey
	 * @return list 
	 */
	public List<? extends Activity> getActivitiesByProcDefKeyAndVersion(String processDefinitionKey,Integer version);

	/**
	 * 获取系统中按照流程名称独一无二的部署流程定义
	 * 获取所有部署的流程定义
	 * @return
	 */
	public List<ProcessDefinition> getAllProcessDefinition();

	public List<ProcessDefinition> getProcDefIgnoreVersion();
	
	/**
	 * 根据流程定义key获取流程定义
	 * @param key 
	 * @return
	 */
	public List<ProcessDefinition> getProcDefByProcDefKey(String key);
	
	/**
	 * 根据流程定义KEY关联删除流程定义
	 * @param key
	 */
	public void deleteProcDefByProcDefKey(String key);
	
	/**
	 * 获取人工任务参与者范围定义
	 * @param taskInstDbid
	 * @param taskActivityName
	 * @return
	 */
	public String getAssigneeRange(Long taskInstDbid, String taskActivityName);
	
	/**
	 * 流程流转（发送）
	 * @param workflowForm
	 */
	public void send(WfBusInstanceTaskForm busInstanceTask);
	
	/**
	 * 获取历史跟踪信息
	 * @param wfBusInstanceId
	 */
	public List<TraceRecord> getTraceInfo(Long wfBusInstanceId);
	
	public List<TraceRecord> getTraceInfo(String executionId);
	
	/**
	 * 保存任务的办理人意见
	 * @param task
	 * @param taskOpinion
	 */
	public void saveTaskOpinion(WfBusInstanceTaskForm busInstanceTask);
	
	/**
	 * * 任务的取回
	 * @param wfBusInstanceId 任务主键
	 * @param loginName 用户登陆名 
	 * @return
	 */
	public boolean takeBackTask(String wfInstanceId,String loginName);
	
	/**
	 * 检测选择用户是否符合流程定义标准，不符合返回错误信息，符合，返回null
	 * @param busInstanceTask
	 * @return
	 */
	public ResultForm validateSelectedAssignee(WfBusInstanceTaskForm busInstanceTask);
	
	/**
	 * 根据流程业务实例ID获取流程业务实例对象
	 * @author 舒军添加
	 * @param wfBusInstanceId 流程业务实例ID
	 */
	public WfBusInstanceTaskForm getWfBusInstanceTaskById(Long taskInstDbid, String userKey);
	
	public String getTaskOpinion(Long taskInstDbid);
	
	/**
	 * 根据流程定义ID获取流程定义
	 * @param processDefinitionId
	 * @return
	 */
	public ProcessDefinitionImpl getProcDefByProcDefId(String processDefinitionId);
	
	/**
	 * 根据ID获取任务对象
	 * @param taskInstDbid
	 * @return
	 */
	public Jbpm4Task getJbpm4TaskById(Long taskInstDbid);
	
	/**
	 * 保存任务签收时间至历史任务
	 */
	public void signTask(Long taskInstDbid);
	
	public void signTasks(Long[] taskInstDbids);
	
	/**
	 * 保存任务签收时间至历史任务
	 */
	public void signTask(Jbpm4Task task);

	public void calcWfSendOption(WfBusInstanceTaskForm form);
	
	/**
	 * 获取历史跟踪信息,先根据节点名称升序，再根据填写意见时间升序来排列
	 * @param wfBusInstanceId
	 */
	public List<TraceRecord> getTraceInfoOrderByActName(Long wfBusInstanceId);
	
	/**
	 * 获取待领取任务列表
	 * @param page
	 * @param templateCode
	 * @param userKey
	 * @param params
	 */
	public void loadCandidateTasksWithBusInfo(Page page, String templateCode, String userKey, Map<String, String> params);
	
	public void takeTasks(Long[] taskInstDbids,String userId);
	
	/**
	 * 回退到上一人工环节
	 */
	public boolean returnPrevTask(Long taskId);
	
	/**
	 * 根据流程实例ID获取流程定义ID
	 * @return 流程定义ID
	 */
	public String getProcessDfIdByProcessInstenceId(String processInstenceId);
	
	public ProcessEngine getProcessEngine();
	
	public WfBusInstanceTaskForm wfBusStart(WfBusTemplateForm template, Map<String, ?> map, String startWfUserKey, Map<String, Object> variables);
	
	/**
	 * 动态创建多工作项
	 * @param assignee
	 * @param taskInstDbid
	 */
	public void createSubMultiTasksInRumtime(String assignee, TaskImpl superTask);
	/**
	 * 根据executionId获取多工作项的superTaskId
	 * @param executionId
	 * @return
	 */
	public TaskImpl getSuperTaskByExecutionId(String executionId);
}